package milkyway.hightech.entry;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Properties;
import java.util.logging.Logger;

import milkyway.hightech.formatter.Formatter;
import milkyway.hightech.formatter.FormatterPlainText;

import java.util.ResourceBundle;

import milkyway.hightech.formatter.FormatterTypes;

/**
 *
 * @author michael
 */
public class AccountStatementService
{
    private static Formatter formatter = null;
    private Properties properties;
    private Map<String, Formatter> formMap = new HashMap<String, Formatter>();

    private static final Logger logger =
        Logger.getLogger(AccountStatementService.class.getName());

    /**
     *
     * @param rb
     * @throws ClassNotFoundException
     * @throws InstantiationException
     * @throws IllegalAccessException
     */
    public AccountStatementService(ResourceBundle rb)
        throws ClassNotFoundException, InstantiationException,
            IllegalAccessException
    {   
        try
        {
            String pkg = "milkyway.hightech.formatter.";
            properties = new Properties();

            String prop = System.getProperty("statement");
            FileInputStream fin = new FileInputStream(prop);
            properties.load(fin);

            Collection<Object> formatterCollection = properties.values();

            Iterator<Object> colItr = formatterCollection.iterator();

            while (colItr.hasNext())
            {
                String formatterName = colItr.next().toString();
                Class clazz = Class.forName(pkg + formatterName);
                Formatter form = (Formatter)clazz.newInstance();
                form.setLocale(rb);
                formMap.put(formatterName, form);
            }
        }

        catch (FileNotFoundException e)
        {
            logger.severe("Propertiefile not found: Change to PlainText\n");
            Formatter form = new FormatterPlainText();
            form.setLocale(rb);
        }

        catch (IOException e)
        {
            logger.severe(e.getMessage());
        }

    }

    /**
     *
     * @param itr
     * @param fileName
     * @param mimeType
     * @param rb
     * @return
     * @throws IOException
     */
    public String getStatement(Iterator<Entry> itr, String fileName,
        FormatterTypes mimeType, ResourceBundle rb) throws IOException
    {
        try
        {
            formatter = formMap.get(mimeType.toString());

            String type = formatter.getMimeType();
            writeStatement(itr, new FileOutputStream(fileName + type));
        }

        catch (IOException e)
        {
            logger.severe(e.getMessage());
        }

        catch (NoSuchElementException e)
        {
            logger.severe("NoSuchElementException: Format statement"
                + "as PlainText\n");
            formatter = new FormatterPlainText();
            formatter.setLocale(rb);
            
            String type = formatter.getMimeType();
            writeStatement(itr, new FileOutputStream(fileName + type));
        }

        return formatter.getFormattedText();
    }

    /**
     *
     * @param itr
     * @param sOut
     * @throws IOException
     */
    private void writeStatement(Iterator<Entry> itr, OutputStream sOut)
        throws IOException
    {
        formatter.cleanUp();

        formatter.writeHead();

        while (itr.hasNext())
            formatter.formatTo(itr.next());

        formatter.writeFoot();

        sOut.write(formatter.getFormattedText().getBytes());
        sOut.close();
    }
}
